package com.our.sys.project1.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.our.sys.common.utils.AppUtil;
import com.our.sys.project1.entity.StockInList;
import com.our.sys.project1.service.StockInListService;

@Controller
@RequestMapping("/stock_in_list")
public class StockInListController {

    private static Logger logger = LoggerFactory.getLogger(StockInListController.class);

    @Autowired
    private StockInListService stockInListService;

    @RequestMapping(value = "/query_stock_in_list")
    public @ResponseBody
    Map<String, Object> queryStockInList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<StockInList> page = new Page<StockInList>(request);
        String orderColumn = request.getParameter("sort");
        String dir = request.getParameter("dir");
        if (StringUtils.isBlank(orderColumn)) {
            page.setOrderBy("inTime");
            page.setOrder(Page.DESC);
        } else {
            page.setOrderBy(orderColumn);
            page.setOrder(dir);
        }

        List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
        String name = request.getParameter("name");
        if (StringUtils.isNotBlank(name)) {
            PropertyFilter filter = new PropertyFilter("LIKES_name_OR_py_OR_wb", name.trim());
            filters.add(filter);
        }

        Page<StockInList> data = stockInListService.queryStockIn(page, filters);
        map = AppUtil.buildJSONDataResponse(data.getResult(), (long) data.getTotalCount());
        return map;
    }

}
